package kov.core.body.zy
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.utils.Dictionary;
	
	import kov.core.BodyState;
	import kov.core.IBody;
	import kov.core.StateCommand;
	import kov.core.body.StateEnum;
	import kov.core.global.Global;
	import kov.core.global.KeyConfig;
	import kov.events.KeyEvent;
	import kov.ui.KeyCombo;
	import kov.ui.KeyDef;
	
	public class ZYBeiZhan extends BodyState
	{
		[Embed("../../../../../assets/zy/beizhan/0.gif")]
		private var Frame0:Class;
		[Embed("../../../../../assets/zy/beizhan/1.gif")]
		private var Frame1:Class;
		[Embed("../../../../../assets/zy/beizhan/2.gif")]
		private var Frame2:Class;
		[Embed("../../../../../assets/zy/beizhan/3.gif")]
		private var Frame3:Class;
		[Embed("../../../../../assets/zy/beizhan/4.gif")]
		private var Frame4:Class;
		[Embed("../../../../../assets/zy/beizhan/5.gif")]
		private var Frame5:Class;
		[Embed("../../../../../assets/zy/beizhan/6.gif")]
		private var Frame6:Class;
		[Embed("../../../../../assets/zy/beizhan/7.gif")]
		private var Frame7:Class;
		[Embed("../../../../../assets/zy/beizhan/8.gif")]
		private var Frame8:Class;
		[Embed("../../../../../assets/zy/beizhan/9.gif")]
		private var Frame9:Class;
		[Embed("../../../../../assets/zy/beizhan/10.gif")]
		private var Frame10:Class;
		[Embed("../../../../../assets/zy/beizhan/11.gif")]
		private var Frame11:Class;
		
		
		
		/**
		 * 构造函数。
		 * @param body
		 * @param priority
		 */		
		public function ZYBeiZhan(body:IBody, priority:int = 1)
		{
			super(StateEnum.BEI_ZHAN, body, priority);
			createRenderer();
			createKeyCombo();
		}
		
		// 创建渲染帧。
		private function createRenderer():void
		{
			var f0:BitmapData = (new Frame0() as Bitmap).bitmapData;
			var f1:BitmapData = (new Frame1() as Bitmap).bitmapData;
			var f2:BitmapData = (new Frame2() as Bitmap).bitmapData;
			var f3:BitmapData = (new Frame3() as Bitmap).bitmapData;
			var f4:BitmapData = (new Frame4() as Bitmap).bitmapData;
			var f5:BitmapData = (new Frame5() as Bitmap).bitmapData;
			var f6:BitmapData = (new Frame6() as Bitmap).bitmapData;
			var f7:BitmapData = (new Frame7() as Bitmap).bitmapData;
			var f8:BitmapData = (new Frame8() as Bitmap).bitmapData;
			var f9:BitmapData = (new Frame9() as Bitmap).bitmapData;
			var f10:BitmapData = (new Frame10() as Bitmap).bitmapData;
			var f11:BitmapData = (new Frame11() as Bitmap).bitmapData;
			this.renderer.push(f0);
			this.renderer.push(f1);
			this.renderer.push(f2);
			this.renderer.push(f3);
			this.renderer.push(f4);
			this.renderer.push(f5);
			this.renderer.push(f6);
			this.renderer.push(f7);
			this.renderer.push(f8);
			this.renderer.push(f9);
			this.renderer.push(f10);
			this.renderer.push(f11);
		}
		
		private var keyStateHash:Dictionary;
		
		// 创建按键。
		private function createKeyCombo():void
		{
			this.keyStateHash = new Dictionary();
			
			// 行走
			this.keyStateHash[Global.key.addKeyCombo(new KeyCombo(Vector.<KeyDef>([KeyDef.create(KeyConfig.UP)])))] = StateEnum.XING_ZOU;
			this.keyStateHash[Global.key.addKeyCombo(new KeyCombo(Vector.<KeyDef>([KeyDef.create(KeyConfig.DOWN)])))] = StateEnum.XING_ZOU;
			this.keyStateHash[Global.key.addKeyCombo(new KeyCombo(Vector.<KeyDef>([KeyDef.create(KeyConfig.LEFT)])))] = StateEnum.XING_ZOU;
			this.keyStateHash[Global.key.addKeyCombo(new KeyCombo(Vector.<KeyDef>([KeyDef.create(KeyConfig.RIGHT)])))] = StateEnum.XING_ZOU;
			
			// 跑
			this.keyStateHash[Global.key.addKeyCombo(new KeyCombo(Vector.<KeyDef>([KeyDef.create(KeyConfig.LEFT), KeyDef.create(KeyConfig.LEFT)])))] = StateEnum.PAO;
			this.keyStateHash[Global.key.addKeyCombo(new KeyCombo(Vector.<KeyDef>([KeyDef.create(KeyConfig.RIGHT), KeyDef.create(KeyConfig.RIGHT)])))] = StateEnum.PAO;
			
			// 跑打
			this.keyStateHash[Global.key.addKeyCombo(new KeyCombo(Vector.<KeyDef>([
				KeyDef.create(KeyConfig.LEFT),
				KeyDef.create(KeyConfig.LEFT),
				KeyDef.create(KeyConfig.A)
			])))] = StateEnum.PAO_DA;
			this.keyStateHash[Global.key.addKeyCombo(new KeyCombo(Vector.<KeyDef>([
				KeyDef.create(KeyConfig.RIGHT),
				KeyDef.create(KeyConfig.RIGHT),
				KeyDef.create(KeyConfig.A)
			])))] = StateEnum.PAO_DA;
			
			// 蹲
			this.keyStateHash[Global.key.addKeyCombo(new KeyCombo(Vector.<KeyDef>([
				KeyDef.create(KeyConfig.DOWN),
				KeyDef.create(KeyConfig.B)
			])))] = StateEnum.DUN;
			
			// 放血
			this.keyStateHash[Global.key.addKeyCombo(new KeyCombo(Vector.<KeyDef>([
				KeyDef.create(KeyConfig.A, KeyDef.DOWN),
				KeyDef.create(KeyConfig.B, KeyDef.DOWN)
			]), false))] = StateEnum.FANG_XUE;
			
			// 打
			this.keyStateHash[Global.key.addKeyCombo(new KeyCombo(Vector.<KeyDef>([
				KeyDef.create(KeyConfig.A)
			])))] = StateEnum.DA;
			
			// 跳
			this.keyStateHash[Global.key.addKeyCombo(new KeyCombo(Vector.<KeyDef>([
				KeyDef.create(KeyConfig.B)
			])))] = StateEnum.TIAO;
			
//			// 突击
//			this.keyStateHash[Global.key.addKeyCombo(new KeyCombo(Vector.<KeyDef>([
//				KeyDef.create(KeyConfig.LEFT),
//				KeyDef.create(KeyConfig.A)
//			])))] = ZhaoYun.TE_JI;
//			this.keyStateHash[Global.key.addKeyCombo(new KeyCombo(Vector.<KeyDef>([
//				KeyDef.create(KeyConfig.RIGHT),
//				KeyDef.create(KeyConfig.A)
//			])))] = ZhaoYun.TE_JI;
		}
		
		override public function execute():void
		{
			this.body.renderer.frameSequence = this.renderer;
			this.body.renderer.delay = 90;
			this.body.renderer.offsetX = 67;
			this.body.renderer.offsetY = 76;
			this.body.renderer.gotoAndPlay(0);
			
			Global.key.addEventListener(KeyEvent.WORK, onKeyWork);
		}
		
		override public function exit():void
		{
			Global.key.removeEventListener(KeyEvent.WORK, onKeyWork);
		}
		
		// 按键事件。
		private function onKeyWork(event:KeyEvent):void
		{
			var stateName:String = this.keyStateHash[event.keyComboID];
			if (stateName)
			{
				var cmd:StateCommand = new StateCommand(stateName);
				this.body.executeStateCommand(cmd);
			}
		}
		
	}
}